<?php

/**
 * media actions.
 *
 * @package    Nika
 * @subpackage media
 * @author     Alexey Shapran (alexey.shapran@gmail.com)
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class mediaActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function postExecute()
	{
		$this->setLayout('gallery');
	}

	public function executeIndex(sfWebRequest $request)
	{
		$howmuchlastcomments = 4;
		$this->allbranches = Branch::retrieveallbranchesidandtitle();
		foreach($this->allbranches as $branch)
		{
			$branch['Album'] = Album::retrieveAlbumsByBranch($branch['id'], 4) -> execute();
		}

		$this->usersalbums = Album::retrieveLastAllUsersAlbums( 2*count($this->allbranches) );  //retrieveLastAllUsersAlbums( 2*count($this->allbranches) );

		if ($this->getUser()->isAuthenticated())
		{
			$query = Album::retrieveUsersAlbumsIds( $this->getUser()->getGuardUser()->getId() );
			$itsalbums = $query->execute();
			foreach($itsalbums as $album){
				$bids[] = $album['id'];
			}

				if(count($itsalbums))
				{
			$this->lastcomments = Comment::retrievelastcomments(implode(',',$bids), $howmuchlastcomments);
				}
		}
	}
	public function executeShowmyalbums(sfWebRequest $request)
	{
		$albumsonpage = 6;
				
		if (!$this->getUser()->isAuthenticated()) $this->redirect('media/index');
		
		$userid = $request->getParameter('userid', $this->getUser()->getGuardUser()->getId());
		$query = Album::retrieveuseralbumquery($userid);
		$this->pager = new sfDoctrinePager('album',$albumsonpage);
		$this->pager->setQuery($query);
		$this->pager->setPage( $request->getParameter('page','1') );
		$this->pager->init();
		
		if ( $userid == $this->getUser()->getGuardUser()->getId() )
		{
		$this->itsmyalbum = true;		
		}
		else
		{ 
		$this->itsmyalbum = false;
		$this->userid = $userid;
		}		

		$howmuchlastcomments = 4;

		$query = Album::retrieveUsersAlbumsIds( $this->getUser()->getGuardUser()->getId());
		$query->execute();
		$itsalbums = $query->execute();
		foreach($itsalbums as $album)
		{
			$bids[] = $album['id'];
		}
		
		if (count($this->$query))
		$this->lastcomments = Comment::retrievelastcomments(implode(',',$bids), $howmuchlastcomments);
	}

	public function executeCreatealbum(sfWebRequest $request)
	{
		$this->form = new AlbumForm();

		if(!$this->getUser()->getGuardUser()->getIsSuperAdmin())
		unset($this->form['branchname']);
	}

	public function executeSubmitalbum(sfWebRequest $request)
	{
		if ($request->isMethod('post'))
		{
			$this->form = new AlbumForm();
			$albtitle = $request->getParameter('title');
			$bra_id = $request->getParameter('branchname');

			$this->form->bind(array(
			'title'			  => ($albtitle == ''? 'без названия' : $albtitle ),
			'is_published'	  => !($request->getParameter('is_published')),
			'user_id'		  => $this->getUser()->getGuardUser()->getId(),
			'branch_id' 	  => ($bra_id == -1? NULL : $bra_id),		
			));

			if ($this->form->isValid())
			{
				$this->form->save();
				$this->getUser()->setFlash('result','Альбом успешно создан');
				$this->redirect('media/showmyalbums');
			}
		}
	}


	public function executeEditalbum(sfWebRequest $request)
	{
		$this->albid = $this->getRequestParameter('aid');
		
		if (!$this->getUser()->isAuthenticated())
		{
			$this->redirect('media/index');
		}elseif ( !$this->getUser()->getGuarduser()->getIsSuperAdmin())
		{
			$this->album = Album::retrieveAlbumToEditByUserIdAndId($this->albid, $this->getUser()->getGuardUser()->getId()) ;
		}
		else {
			$this->album = Album::retrieveAlbumToEditById($this->albid);
		}
		
		
		if ($this->album)
		{
			$this->allmedias = Media::retrieveAllMediaFromAlbumByAlbumId($this->albid);
			$this->photoform = new MediaForm();
		}
	}


	public function executeDeletealbum(sfWebRequest $request)
	{
		$aid = $this->getRequestParameter('aid');
		$userid = $this->getUser()->getGuardUser()->getId();
		$delquery = Album::retrievealbumtodeletebyidanduserid($aid, $userid);
		$delquery -> execute();

		$this->getUser()->setFlash('result','Альбом успешно удален');
		$this->redirect('media/showmyalbums');
	}


	public function executeAddpicturetoalbum(sfWebRequest $request)
	{
		$this->form = new MediaForm();

		if ($request->isMethod('post'))
		{
			$imagefile = $request->getFiles('image');
			$albumid = $request->getParameter('albumid');
			$this->form->bind(array('title'       => strip_tags($request->getParameter('title')),
								  	'media_type'  => 'photo',
								  	'image' 	  => $request->getParameter('image'),
								  	'user_id'     => $this->getUser()->getGuardUser()->getId(),
									'album_id'    => $albumid),
			array('image' => $imagefile)
			);

			if ($this->form->isValid())
			{
				$this->form->save();
				$this->getUser()->setFlash('done','Фотография загружена');
			}
		}

		$this->allmedias = Media::retrieveAllMediaFromAlbumByAlbumId($albumid);
		$this->redirect('media/editalbum?aid='. $albumid);
	}
	public function executeDeletepicture(sfWebRequest $request)
	{
		$pict = $request->getParameter('pictid');
		$req = Media::retrieveMediaFileById($pict);
		$req->delete('media');
		$req->execute();
		return sfView::NONE;
	}
	public function executeChangetitle(sfWebRequest $request)
	{
		$pict = $request->getParameter('pictid');
		$req = Media::retrieveMediaFileById($pict);
		$req->from('media');
		$pict = $req->fetchOne();
		$pict['title'] = $request->getParameter('newtitle');
		$pict->save();

		return sfView::NONE;
	}
	public function executeMakeclosed(sfWebRequest $request)
	{
		$aid = $this->getRequestParameter('albumid');
		$album = Album::retrieveAlbumToEditById($aid);
		
		if ($album->getUserId() == $this->getUser()->getGuardUser()->getId() || ($album['branch_id']!= NULL && $this->getUser()->isSuperAdmin()))
		{
		$album->setIsPublished(1-$album->getIsPublished());
		$album->save();
		$this->redirect('media/editalbum?aid='.$aid);
		}
		else $this->redirect('media/index');		
	}
	public function executeSetupcover(sfWebRequest $request)
	{
		$aid = $request->getParameter('aid');
		Album::updatecover( $aid, $this->getUser()->getGuardUser()->getId(), $request->getParameter('pictid'));
		//$fromwhere = $_SERVER['HTTP_REFERER'];
		//$this->redirect($fromwhere);
		$this->redirect('media/editalbum?aid='.$aid);
	}

	public function executeShowalbum(sfWebRequest $request)
	{
		$this->albid = $this->getRequestParameter('aid');
		$this->album = Album::retrieveAlbumToShow($this->albid);
		
			if ($this->album['is_published'] == 1)
			{
		if (!empty($this->album))
		{
			$this->allmedias = Media::retrievePhotoOrVideoFromAlbumByAlbumId($this->albid,'photo');
		}
			}
			elseif(!$this->getUser()->isAuthenticated())
			{				
				$this->setTemplate('shownotpublished');
			}
	}
	public function executeShowvideoalbums(sfWebRequest $request)
	{
		$this->allalbums = Album::retrieveallalbumshasvideo();

	}
	public function executeAddvideotoalbum(sfWebRequest $request)
	{
		$this->media = new Media();
		$this->media['user_id'] = $this->getUser()->getGuardUser()->getId();
		$this->media['video'] = $request->getParameter('youtubelink');
		$this->media['media_type'] = 'video';
		$this->media['title'] = $request->getParameter('title');
		$this->media['album_id'] = $request->getParameter('albumid');

		$this->media->save();

		/* if ($request->isMethod('post'))
		 {
			$albumid = $request->getParameter('albumid');
			$this->form->bind(array('title'       => $request->getParameter('title'),
			'media_type'  => 'video',
			'video' 	  => $request->getParameter('youtubelink'),
			'user_id'     => $this->getUser()->getGuardUser()->getId(),
			'album_id'    => $albumid)
			);

			$this->form->save();
			} */

		return sfView::NONE;
	}
	public function executeShowvideoalbum(sfWebRequest $request) 
	{
		$this->albid = $this->getRequestParameter('aid');
		$this->album = Album::retrieveAlbumToShow($this->albid);
			
		if (!empty($this->album))
		{
			$this->query = Media::retrieveVideoQueryByAlbumId($this->albid,'video');
			$this->pager = new sfDoctrinePager('Media',1);
			$this->pager->setQuery($this->query);
			$this->pager->setPage($request->getParameter('page',1));
			$this->pager->init();
		}	
	}
	public function executeShowbranchalbums(sfWebRequest $request)
	{
		$br_id = $request->getParameter('brid');
		
		$this->branch = Branch::retrievebranchbyid($br_id);
		
		$maxalbsperpageinbranch = 18;
		
		$this->pager = new sfDoctrinePager('Album',$maxalbsperpageinbranch);
		$this->pager->setQuery(Album::retrieveAlbumsByBranch($br_id));
		$this->pager->setPage($request->getParameter('page',1));
		$this->pager->init();	
	}
	public function executeChangelink(sfWebRequest $request)
	{
		//error_log("MEDIAID = ".$request->getParameter('mediaid'));
		
			$video = Media::retrieveMediaFileById($request->getParameter('mediaid'));
			$vf = $video->fetchOne();
			
		if ( $vf['user_id'] == $this->getUser()->getGuardUser()->getId() || $this->getUser()->getGuardUser()->getIsSuperAdmin())
		{
			$vf->setVideo($request->getParameter('newlink'));
			$vf->save();
		}
		
		return sfView::NONE;
	}
}
